package cn.sdack.go.users.controller;

import cn.sdack.go.common.entities.BaseEntity;
import cn.sdack.go.common.entities.JpaPage;
import cn.sdack.go.common.entities.JsonResult;
import cn.sdack.go.common.entities.users.AuthorityEntity;
import cn.sdack.go.common.entities.users.RoleEntity;
import cn.sdack.go.common.querys.users.RoleQuery;
import cn.sdack.go.users.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author sdack
 * @date 2024/1/9
 */
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/role/page")
    public JsonResult<?> query(@RequestParam(name = "page",defaultValue = "0") int page
            , @RequestParam(name = "pageSize",defaultValue = "0") int pageSize, @RequestBody RoleQuery param) {

        try {
            Page<RoleEntity> res = roleService.queryAll(param, page, pageSize);
            return JsonResult.toJson(JpaPage.to(res));
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }


    @GetMapping("/role/{id}")
    public JsonResult<?> getByid(@PathVariable("id") Long id) {
        try {
            RoleEntity entity = roleService.getById(id);
            return JsonResult.toJson(entity);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @PostMapping("/role")
    public JsonResult<?> add(@RequestBody @Validated(BaseEntity.Add.class) RoleEntity param, BindingResult errors) {
        if (errors.hasErrors()){
            return JsonResult.toJson(false,errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            RoleEntity res = roleService.add(param);
            return JsonResult.toJson((res));
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @PutMapping("/role")
    public JsonResult<?> set(@RequestBody @Validated(BaseEntity.Set.class) RoleEntity param, BindingResult errors) {
        if (errors.hasErrors()) {
            return JsonResult.toJson(false,errors.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            roleService.getById(param.getId());
            RoleEntity res = roleService.set(param);
            return JsonResult.toJson((res));
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

    @DeleteMapping("/role/{id}")
    public JsonResult<?> del(@PathVariable("id") Long id) {
        try {
            roleService.getById(id);
            roleService.del(id);
            return JsonResult.toJson(true);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }


    @PostMapping("/role/authority")
    public JsonResult<?> authority(@RequestBody RoleQuery param) {
        if (param.getAuthority() == null || param.getAuthority().isEmpty()) {
            return JsonResult.toJson(false,"权限必须");
        }
        if (param.getCode() == null) {
            return JsonResult.toJson(false,"角色Code 必须");
        }
        if (param.getGid() == null) {
            return JsonResult.toJson(false,"组织 必须");
        }
        try {
            List<AuthorityEntity> list = param.getAuthority().stream().filter(Objects::nonNull).map(it -> AuthorityEntity.builder().code(it).name("").build()).collect(Collectors.toList());
            roleService.authority(param.getGid(),param.getCode(),list);
            return JsonResult.toJson(true);
        }catch (Exception e) {
            String message = e.getMessage().toString();
            if (message.length() > 100) {
                message = message.substring(0,100);
            }
            return JsonResult.toJson(false,message);
        }
    }

}
